//package com.youlv.yl.interceptor;
//
//import com.youlv.yl.config.RedisKeyConfig;
//import com.youlv.yl.config.SystemConfig;
//import com.youlv.yl.util.JedisUtils;
//import com.youlv.yl.util.StringUtils;
//import lombok.extern.slf4j.Slf4j;
//import org.codehaus.jettison.json.JSONObject;
//import org.springframework.http.HttpMethod;
//import org.springframework.lang.Nullable;
//import org.springframework.stereotype.Component;
//import org.springframework.web.servlet.HandlerInterceptor;
//import org.springframework.web.servlet.ModelAndView;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.PrintWriter;
//
///**
// * @author fenghe
// * @date 2022/1/16 18:43
// */
//
//@Component
//@Slf4j
//public class LoginInterceptor implements HandlerInterceptor {
//
//    /**
//     * 这个方法是在访问接口之前执行的，我们只需要在这里写验证登陆状态的业务逻辑，就可以在用户调用指定接口之前验证登陆状态了
//     */
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
//
//       synchronized (Object.class) {
//           crossDomain(request, response);
//           //输出 OPTIONS/GET/POST。。。
//           String method = request.getMethod();
//           //如果是 OPTIONS 请求，让 OPTIONS 请求返回一个200状态码
//           if (HttpMethod.OPTIONS.toString().equals(request.getMethod())) {
//               response.setStatus(HttpServletResponse.SC_OK);
//               //放行
//               return true;
//           } else {
//               String uri = request.getRequestURI();
//               System.out.println(uri);
//               // 获取令牌
//               String token = request.getHeader(SystemConfig.REQ_HEADER_TOKEN);
//               // 校验是否传递令牌
//               if (StringUtils.isNotEmpty(token) && JedisUtils.exists(RedisKeyConfig.USER_TOKEN + token)) {
//                   // 验证令牌是否存在
//                   // 判断传递的令牌是否正确(验证多设备唯一登陆)若不正确，拒绝请求
//                   // 查询在线用户手机号
//                   String phone = JedisUtils.getHash(RedisKeyConfig.USER_UID_PHONE,
//                           JedisUtils.getString(RedisKeyConfig.USER_TOKEN + token));
//                   if(JedisUtils.getString(RedisKeyConfig.USER_PHONE + phone).equals(token)) {
//                       // 存在令牌，则续命
//                       JedisUtils.expire(RedisKeyConfig.USER_TOKEN + token, RedisKeyConfig.USER_TOKEN_TIME);
//
//                       // 手机号的key也需要续命
//                       JedisUtils.expire(RedisKeyConfig.USER_PHONE + phone, RedisKeyConfig.USER_TOKEN_TIME);
//                       //放行
//                       return true;
//                   } else {
//                       response.setCharacterEncoding("UTF-8");
//                       response.setContentType("application/json; charset=utf-8");
//                       PrintWriter out = null;
//                       JSONObject res = new JSONObject();
//                       try {
//                           res.put("code", 400);
//                           res.put("msg", "亲，您的账号已在其他设备登陆！！！");
//                           out = response.getWriter();
//                           out.append(res.toString());
//                       } catch (Exception e) {
//                           e.printStackTrace();
//                       }
//                       return false;
//                   }
//               } else if(uri.startsWith(SystemConfig.FILTER_LOGIN) || uri.startsWith(SystemConfig.FILTER_SMS) || uri.endsWith(SystemConfig.FILTER_SWAGGER_1) || uri.startsWith(SystemConfig.FILTER_SWAGGER_2) || uri.startsWith(SystemConfig.FILTER_SWAGGER_3) || uri.startsWith(SystemConfig.FILTER_SWAGGER_4) || uri.startsWith(SystemConfig.FILTER_SWAGGER_5)) {
//                   //放行
//                   return true;
//               } else {
//                   response.setCharacterEncoding("UTF-8");
//                   response.setContentType("application/json; charset=utf-8");
//                   PrintWriter out = null;
//                   JSONObject res = new JSONObject();
//                   try {
//                       res.put("code", 400);
//                       res.put("msg", "亲，请重新登陆");
//                       out = response.getWriter();
//                       out.append(res.toString());
//                   } catch (Exception e) {
//                       e.printStackTrace();
//                   }
//                   return false;
//               }
//           }
//       }
//    }
//
//    @Override
//    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) {
//    }
//
//    @Override
//    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) {
//    }
//
//    public void crossDomain(HttpServletRequest request, HttpServletResponse response) {
//        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
//        response.setHeader("Access-Control-Allow-Credentials", "true");
//    }
//}
